package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.GiftDao;
import guda.shop.cms.dao.ShopMemberDao;
import guda.shop.cms.entity.Gift;
import guda.shop.cms.entity.ShopMember;
import guda.shop.cms.manager.GiftMng;
import guda.shop.common.hibernate3.Updater;
import guda.shop.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GiftMngImpl
        implements GiftMng {

    @Autowired
    private GiftDao giftDao;

    @Autowired
    private ShopMemberDao shopMemberDao;

    public Gift deleteById(Long paramLong) {
        return this.giftDao.deleteById(paramLong);
    }

    public Gift[] deleteByIds(Long[] paramArrayOfLong) {
        Gift[] arrayOfGift = new Gift[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfGift[i] = deleteById(paramArrayOfLong[i]);
            i++;
        }
        return arrayOfGift;
    }

    public Gift findById(Long paramLong) {
        return this.giftDao.findById(paramLong);
    }

    public Pagination getPageGift(int paramInt1, int paramInt2) {
        return this.giftDao.getPageGift(paramInt1, paramInt2);
    }

    public Gift save(Gift paramGift) {
        return this.giftDao.save(paramGift);
    }

    public Gift updateByGiftnumb(Long paramLong1, Integer paramInteger, Long paramLong2) {
        Gift localGift = this.giftDao.findById(paramLong1);
        ShopMember localShopMember = this.shopMemberDao.findById(paramLong2);
        Integer localInteger1 = localGift.getGiftStock();
        Integer localInteger2 = Integer.valueOf(localGift.getGiftScore().intValue() * paramInteger.intValue());
        if (localInteger1.intValue() < paramInteger.intValue())
            return null;
        if (localInteger2.intValue() > localShopMember.getScore().intValue())
            return null;
        localGift.setGiftStock(Integer.valueOf(localInteger1.intValue() - paramInteger.intValue()));
        localShopMember.setScore(Integer.valueOf(localShopMember.getScore().intValue() - localInteger2.intValue()));
        return localGift;
    }

    public Gift updateByUpdater(Gift paramGift) {
        Updater localUpdater = new Updater(paramGift);
        return this.giftDao.updateByUpdater(localUpdater);
    }
}

